package com.baizhi.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baizhi.common.dto.Product;
import com.baizhi.common.enums.AckCode;
import com.baizhi.common.vo.PageVO;
import com.baizhi.common.vo.R;
import com.baizhi.vos.CollectionVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class ProductController {

    private static final Logger log = LoggerFactory.getLogger(ProductController.class);

    @Value("${server.port}")
    private int port;



    @GetMapping("/productList2")
    public R<PageVO<Product>> findByCategoryIdAndPage2(Integer page, Integer rows, Integer categoryId) throws InterruptedException {
        log.info("当前页: {} 每页显示记录数:{} 当前类别id:{} ",page,rows,categoryId);


            List<Product> products = new ArrayList<>();
            products.add(new Product(1,"短裙",23.23,new Date()));
            products.add(new Product(2,"超短裙",23.23,new Date()));
            products.add(new Product(3,"超级超短裙",23.23,new Date()));
            int total = 1000;

            PageVO<Product>  pageVO=new PageVO<>();
            pageVO.setTotal(1000L);
            pageVO.setRows(products);

            return R.okHasData(pageVO);

    }

    @GetMapping("/productList")
    public R<Map<String,Object>> findByCategoryIdAndPage(Integer page, Integer rows, Integer categoryId){
        log.info("当前页: {} 每页显示记录数:{} 当前类别id:{} ",page,rows,categoryId);
        //根据类别id分页查询符合当前页集合数据  List<Product>   select * from t_product where categoryId=? limt ?(page-1)*rows,?(rows)
        //根据类别id查询当前类别下总条数       totalCount           select count(id) from t_product where categoryId=?
        Map<String, Object> map = new HashMap<>();
        List<Product> products = new ArrayList<>();
        products.add(new Product(1,"短裙",23.23,new Date()));
        products.add(new Product(2,"超短裙",23.23,new Date()));
        products.add(new Product(3,"超级超短裙",23.23,new Date()));
        int total = 1000;
        map.put("rows",products);
        map.put("total", total);
        return R.okHasData(map);
    }

    //报  ParamFlowException 异常
    @SentinelResource(value = "producthot")
    @GetMapping("/products/{categoryId}")
    public R findByCategoryId(@PathVariable(value = "categoryId") Integer categoryId){
        log.info("类别id: {}",categoryId);
        //调用业务逻辑根据类别id查询商品列表
        List<Product> products = new ArrayList<>();
        products.add(new Product(1,"短裙",23.23,new Date()));
        products.add(new Product(2,"超短裙",23.23,new Date()));
        products.add(new Product(3,"超级超短裙",23.23,new Date()));
        return R.okHasData(products);
    }




    //定义一个接口接受集合类型参数
    //springmvc 不能直接接受集合类型参数,如果想要接收集合类型参数必须将集合放入对象中,使用对象的方式接收才行
    //oo: oriented(面向) object(对象) 面向对象
    // vo(value object): 用来传递数据对象称之为值对象
    // dto:(data transfer(传输) object):数据传输对象
    @GetMapping("/test4")
    public R test4(CollectionVO collectionVO){
        collectionVO.getIds().forEach(id-> log.info("id:{} ",id));
        return  R.okHasData("test4 ok,当前服务端口为: "+port);
    }

    //定义个接口接受数组类型参数
    @GetMapping("/test3")
    public R test3(String[] ids){
        for (String id : ids) {
            log.info("id: {}",id);
        }
        //手动转为list List<String> strings = Arrays.asList(ids);
        return R.okHasData("test3 ok,当前服务端口为: "+port);
    }

    //定义一个接受对象类型参数接口
    @PostMapping("/test2")
    public R test2(@RequestBody Product product){
        log.info("product:{}",product);
        return  R.okHasData("test2 ok,当前服务端口为: "+port);
    }

    //定义一个接受零散类型参数接口  路径传递参数
    @GetMapping("/test1/{id}/{name}")
    public R test1(@PathVariable("id") Integer id,@PathVariable("name") String name){
        log.info("id:{}    name:{}",id,name);
        return R.okHasData("test1 ok,当前服务端口为: "+port);
    }

    //定义一个接受零散类型参数接口 queryString
    @GetMapping("/test")
    public  R test(String name, Integer age){
        log.info("name:{}     age:{}",name,age);
        return R.okHasData( "test ok,当前服务端口为: "+port);
    }




    @GetMapping("/list")
    public R list(HttpServletRequest request,String color){
        String header = request.getHeader("User-Name");
        System.out.println("获取对应请求参数 color: "+color);
        System.out.println("获取请求头信息: "+header);
        log.info("商品列表服务");
        return  R.okHasData("list ok当前提供服务端口:"+port);
    }
}
